﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AppToolKit.Data;
using Common.Logging;
using System.Runtime.Remoting.Messaging;
using System.Data;

namespace App.Service
{
    public class DatabaseService : IDbProcessor
    {
        private static ILog logger_ = LogManager.GetLogger(typeof(DatabaseService));
        
        private IDbOperation template_;
        public IDbConnection CreateConnection()
        {
            return DbOperation.CreateConnection();
            //return db_.CreateConnection();
        }


        public IDbDataAdapter GetDataAdapter()
        {
            return DbOperation.GetDataAdapter();
        }


        public void LoadDataSet(CommandType commandType, string commandText, ref DataSet dataSet, string[] tableNames)
        {
            //DataSet ds = new DataSet();
            //ds.Tables.Add();

           // if (msg_ != null) msg_.SendMessage(commandText);
            DbOperation.LoadDataSet(commandType, commandText, ref dataSet, tableNames);
            //msg_.SendMessage(dataSet.Tables[0].Rows.Count.ToString());
            //template_.ClassicAdoTemplate.DataSetFill(ds, commandType, commandText, new string[] { ds.Tables[0].TableName });

            //dataSet.Tables.Add();
            //return null;
            //return ds;
        }


        public DataSet ExecuteDataSet(CommandType commandType, string commandText)
        {
          //  if (msg_ != null) msg_.SendMessage(commandText);
            return DbOperation.ExecuteDataSet(commandType, commandText);
            //return db_.ExecuteDataSet(commandType, commandText);
        }


        public object ExecuteScalar(CommandType commandType, string commandText)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(commandText);
            return DbOperation.ExecuteScalar(commandType, commandText);
        }

        public int ExecuteNonQuery(CommandType commandType, string commandText)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(commandText);
            return DbOperation.ExecuteNonQuery(commandType, commandText);
        }

        public IDataReader ExecuteReader(CommandType commandType, string commandText)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(commandText);
            return DbOperation.ExecuteReader(commandType, commandText);
        }

        public int ExecuteNonQuery(string storedProcedureName, params object[] parameterValues)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(storedProcedureName);
            return DbOperation.ExecuteNonQuery(storedProcedureName, parameterValues);
        }

        public void ExecuteReader(CommandType commandType, string commandText, ExecuteReaderCallback callback)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(commandText);
            //DbProcessor.ExecuteReader(commandType, commandText, callback);
        }

        public void ExecuteReader(CommandType commandType, string commandText, ExecuteReaderCallbackWrapper callbackWrapper)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(commandText);
            DbOperation.ExecuteReader(commandType, commandText, callbackWrapper);
        }

        public void UpdateDataSet(string selectSql, DataSet ds)
        {
            DbOperation.UpdateDataSet(selectSql, ds);
        }

        public int ExecuteNonQuery(IDbQuery wrapper)
        {
            return DbOperation.ExecuteNonQuery(wrapper);
        }

        public DataSet ExecuteDataSet(IDbQuery wrapper)
        {
            return DbOperation.ExecuteDataSet(wrapper);
        }

        public void ExecuteReader(IDbQuery dbQuery, ExecuteReaderCallbackWrapper callback)
        {
            DbOperation.ExecuteReader(dbQuery, callback);
        }

        public object ExecuteScalar(IDbQuery wrapper)
        {
            return DbOperation.ExecuteScalar(wrapper);
        }

        public ICommandBuilder GetCommandBuilder()
        {
            return DbOperation.GetCommandBuilder();
        }

        //public void ExecuteReader(CommandType commandType, string commandText, ExecuteReaderCallback callback)
        //{
        //    if (msg_ != null)
        //        msg_.SendMessage(commandText);
        //    DbProcessor.ExecuteReader(commandType, commandText, callback);
        //}

        public DataSet ExecuteDataSet(string storedProcedureName, params object[] parameterValues)
        {
            //if (msg_ != null)
            //    msg_.SendMessage(storedProcedureName);
            return DbOperation.ExecuteDataSet(storedProcedureName, parameterValues);
        }

        public string Escape(string sql)
        {
            return sql.Replace("'", "''");
        }


        //public IMessage Msg
        //{
        //    get { return msg_; }
        //    set { msg_ = value; }
        //}

        public IDbOperation DbOperation
        {
            get { return template_; }
            set { template_ = value; }
        }
        /// <summary>
        /// 测试连接
        /// </summary>
        /// <returns></returns>
        public bool TestConnect()
        {
            try
            {
                DbOperation.CreateConnection().Open();
                DbOperation.CreateConnection().Close();
                return true;
            }
            catch (Exception ex)
            {
                logger_.Error(ex.Message, ex);
                return false;
            }

        }
    }
}